window.sessionStorage.setItem('vouchers-type',0);
var pro_load_page = 2,pro_load_flag = true;
var voucherSearch={
	init:function(data){
		app.ajax(
			'/Member/Coupon/index',
			data,
			function(res){
				if(res.code == 200){
						app.tpl($('.voucher-list'),'tpl-voucher-list',res);
						$('.list .line:last').hide();
				}else{
					layer.msg('网络繁忙，请稍后再试');
				}
		});
	},
	loadMore : function(url,data){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.voucher-list').append(listLoadingEmpty);
		pro_load_flag = false;
		app.ajax(
			url,
			data,
			function(res){
				$('.voucher-list .no-more').remove();
				if(res.code == 200){
					if(res.list.length > 0){
						app.tpladd($('.voucher-list'),'tpl-voucher-list',res);	
						pro_load_page++;
						pro_load_flag = true;
					//page++;
					}else{						
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.voucher-list').append(listLoadingEmpty);
					}
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);	
	}
}
	footer.init();
	header.init('使用代金劵');
	voucherSearch.init({
		status : 0,
		page :1,
		cpns_key : '' //搜索代金劵
	});

window.onload=function(){
	if($('header').html()){
		$('.explain').css('marginTop','7.34rem');
	}
	
	$('body').on('click','.sure-search',function(){
		var keywords = $(this).parent('.mui-row').find('input.keyword').val();
		window.sessionStorage.setItem('keywords',keywords);
		console.log(keywords);
		voucherSearch.init({
			status : 0,
			page :1,
			cpns_key : keywords
		})
	});
	
	$('body').on('click','.voucher-list .list',function(){
	var ids = $(this).attr('data-id'),
		then = $(this).attr('data-then'),
		data = {
			id :ids,
			then :then
		};
		data=JSON.stringify(data);
		window.sessionStorage.setItem('acconttovouchers',data);
		history.go(-1);
	});
	
	
	$(window).scroll(function(){
				
		if($('.voucher-list img').attr('src')){
			return false;
		}
		// 当滚动到距底部以上200像素
		var offset = $(".voucher-list").offset(),
		data = {};
		var documentHeight = offset.top + $(".voucher-list").height();
		if (documentHeight - $(this).scrollTop() - $(this).height()<100){
			var key = window.sessionStorage.getItem('keywords');
			data ={
				status : 0,
				page   : pro_load_page,
				cpns_key : key
			}
			pro_load_flag && voucherSearch.loadMore('/Member/Coupon/index',data);
		}
	});
};
